package com.cse.config;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.cse.htmlparser.refactor.yixun.SiteModel;

public class DomXML {
	public static List<SiteModel> getBooks(String siteNme) throws Exception{
	  List<SiteModel> list = new ArrayList<SiteModel>();
	  InputStream input = DomXML.class.getResourceAsStream("/property.xml");
	  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
      DocumentBuilder builder = factory.newDocumentBuilder();  
      Document document = builder.parse(input);  
      Element element = document.getDocumentElement();  
      NodeList ndoelNodes = element.getElementsByTagName("site");  

      for(int i=0;i<ndoelNodes.getLength();i++){
          Element modelElement = (Element) ndoelNodes.item(i);
          if(modelElement.getAttribute("id").equals(siteNme)){
        	  NodeList childNodes = modelElement.getChildNodes();
        	  for(int j=0;j<childNodes.getLength();j++){
        		Node item = childNodes.item(j);
        		if(item.getNodeName().equals("item")){
        			NodeList itemlist = item.getChildNodes();
        			SiteModel model = new SiteModel();
        			for(int index = 0;index<itemlist.getLength();index++){
        				if(itemlist.item(index).getNodeName().equals("html")){
        					model.setHtml(itemlist.item(index).getFirstChild().getNodeValue());
        				}if(itemlist.item(index).getNodeName().equals("file")){
        					model.setFile(itemlist.item(index).getFirstChild().getNodeValue());
        				}
        			}
        			list.add(model);
        		}
          	  }
      	}
      }
      return list;
    }
	
	
	public static void main(String[] args){
		List<SiteModel> lists = new ArrayList<SiteModel>();
		try {
			lists = DomXML.getBooks("yixun");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		for(SiteModel model : lists){
			System.out.println(model.getFile());
			System.out.println(model.getHtml());
		}
	
		
	}

}
